package com.ruoyi.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.app.controller.student.vo.AppStudentUpdateReqVo;
import com.ruoyi.app.mapper.AppStudentMapper;
import com.ruoyi.app.service.IAppStudentService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.app.controller.student.vo.AppStudentCreateReqVo;
import com.ruoyi.app.controller.student.vo.AppStudentRespVo;
import com.ruoyi.app.domain.AppStudent;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 学生Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-11
 */
@Service
public class AppStudentServiceImpl extends ServiceImpl<AppStudentMapper, AppStudent> implements IAppStudentService {
    @Autowired
    private AppStudentMapper schStudentMapper;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private SysUserRoleMapper userRoleMapper;

    /**
     * 查询学生
     *
     * @param id 学生主键
     * @return 学生
     */
    @Override
    public AppStudentRespVo selectAppStudentById(Long id) {
        AppStudent entity = schStudentMapper.selectById(id);
        AppStudentRespVo vo = new AppStudentRespVo();

        SysUser user = userService.selectUserById(entity.getUserId());
        BeanUtil.copyProperties(user, vo);
        vo.setIdCard(entity.getIdCard());
        return vo;
    }

    /**
     * 查询学生列表
     *
     * @param schStudent 学生
     * @return 学生
     */
    @Override
    public List<AppStudentRespVo> selectAppStudentList(AppStudent schStudent) {
        MPJLambdaWrapper<AppStudent> lqw = new MPJLambdaWrapper<AppStudent>()
                .selectAll(AppStudent.class)
                .select(SysUser::getUserName, SysUser::getNickName,SysUser::getEmail,
                        SysUser::getPhonenumber,SysUser::getSex,SysUser::getAvatar,SysUser::getPassword)
                .leftJoin(SysUser.class,SysUser::getUserId,AppStudent::getUserId)
                .likeIfExists(SysUser::getNickName,schStudent.getNickName())

                ;
        return schStudentMapper.selectJoinList(AppStudentRespVo.class,lqw);
    }

    private LambdaQueryWrapper<AppStudent> buildQueryWrapper(AppStudent schStudent) {
        LambdaQueryWrapper<AppStudent> lqw = Wrappers.lambdaQuery();
        lqw.eq(schStudent.getUserId() != null, AppStudent::getUserId, schStudent.getUserId());
        return lqw;
    }

    /**
     * 新增学生
     *
     * @param schStudent 学生
     * @return 结果
     */
    @Override
    public int insertAppStudent(AppStudentCreateReqVo schStudent) {
        //系统用户
        SysUser sysUser = new SysUser();
        BeanUtil.copyProperties(schStudent, sysUser);
        sysUser.setCreateBy(SecurityUtils.getUsername());
        sysUser.setPassword(SecurityUtils.encryptPassword(schStudent.getPassword()));
        userMapper.insertUser(sysUser);

        //插入角色
        List<SysUserRole> list = new ArrayList<SysUserRole>(1);

        SysUserRole ur = new SysUserRole();
        ur.setUserId(sysUser.getUserId());
        ur.setRoleId(100L);
        list.add(ur);
        userRoleMapper.batchUserRole(list);

        AppStudent schStudent1 = new AppStudent();
        schStudent1.setUserId(sysUser.getUserId());
        schStudent1.setIdCard(schStudent.getIdCard());
        return schStudentMapper.insert(schStudent1);
    }

    /**
     * 修改学生
     *
     * @param schStudent 学生
     * @return 结果
     */
    @Override
    public int updateAppStudent(AppStudentUpdateReqVo schStudent) {
        SysUser user = userService.selectUserById(schStudent.getUserId());
        BeanUtil.copyProperties(schStudent, user);
        AppStudent appStudent = schStudentMapper.selectOne(new LambdaQueryWrapper<AppStudent>().eq(AppStudent::getUserId, schStudent.getUserId()));
        appStudent.setNickName(user.getNickName());
        appStudent.setIdCard(schStudent.getIdCard());
        schStudentMapper.updateById(appStudent);
        return userMapper.updateUser(user);
    }

    /**
     * 批量删除学生
     *
     * @param ids 需要删除的学生主键
     * @return 结果
     */
    @Override
    public int deleteAppStudentByIds(Long[] ids) {
        return schStudentMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除学生信息
     *
     * @param id 学生主键
     * @return 结果
     */
    @Override
    public int deleteAppStudentById(Long id) {
        return schStudentMapper.deleteById(id);
    }
}
